angular.module('panels')
    .directive("panelItilTododealCount", function ($http, loadConf, $timeout, $log, moment, Promise, LoginInfo) {
        return {
            scope: {},
            link: function (scope) {

                function gradientColor(color) {
                    return {
                        radialGradient: {cx: 0.5, cy: 0.3, r: 0.7},
                        stops: [
                            [0, color],
                            [1, Highcharts.Color(color).brighten(-0.1).get('rgb')] // darken
                        ]
                    };
                }

                function getHighChartOptions(store, categories) {
                    return {
                        chart: {
                            type: 'column'
                        },
                        title: {
                            text: ''
                        },
                        xAxis: {
                            categories: categories
                        },
                        yAxis: {
                            min: 0,
                            title: {
                                text: '工单数',
                                enabled: false
                            }
                        },
                        legend: {
                            reversed: true
                        },
                        plotOptions: {
                            series: {
                                stacking: 'normal'
                            }
                        },
                        series: [{
                            name: '未处理工单',
                            data: store
                        }]
                    };
                }

                scope.options = getHighChartOptions([], []);
                scope.panelTitle = '未处理工单统计';

                Promise.all({
                    setting: loadConf('panel-itil-tododeal-count.yml'),
                    loginInfo: LoginInfo
                }).then(function (result) {
                    var setting = result.setting, loginInfo = result.loginInfo, preData, timeoutId;
                    scope.more = setting.more;
                    scope.$on('$destroy', function () {
                        timeoutId && $timeout.cancel(timeoutId);
                    });
                    (function updateData() {
                        $http.jsonp(setting.countQueryUrl.replace('{userId}', loginInfo.currentUser.LOGIN_ID), {params: {date: moment().format('YYYY-MM-DD')}}).success(function (data) {
                            if (angular.equals(data, preData)) {
                                return false;
                            } else {
                                preData = data;
                                data = angular.copy(data);
                            }

                            var store = [], categories = [];
                            for (var i = 0; i < data.length; i++) {
                                var processObj = data[i];
                                categories.push(processObj["processName"]);
                                store.push(parseInt(processObj["tododealCount"]) || 0);
                                // 限制列数
                                if (categories.length == (setting.maxColumnsAllowed || 5)) break;
                            }

                            // 通知更新charts
                            scope.options = getHighChartOptions(store, categories);
                            if (categories.length > 7) {
                                // 如果类型多于7个，横坐标lebel倾斜45度
                                scope.options.xAxis.labels = {
                                    rotation: -45
                                }
                            }
                        }).then(callback, callback);
                        function callback() {
                            if (setting.updateInterval > 0) {
                                timeoutId = $timeout(updateData, setting.updateInterval * 1000);
                            }
                        }

                        $log.debug('panel update data: itil-tododeal-count');
                    }());
                });
            },
            templateUrl: 'app/panel/highcharts-default.html'
        };
    });